package com.demo.datastructure.queue;

/**
 * @description: 链表实现队列
 * @author: ljr
 * @time: 2021/4/28 13:23
 */
public class LinkedQueue<E> implements MyQueue<E> {

    private Node<E> frist = null;
    private Node<E> last = null;
    private int size;

    @Override
    public boolean enqueue(E value) {
      Node<E> node = new Node<>(value,null);
      if(size++ == 0){
          frist = last = node;
          return true;
      }
      last = last.next = node;
      return true;
    }

    @Override
    public E dequeue() {
        if(size-- == 0){
            throw new RuntimeException("队列中没有数据");
        }
        E value = frist.value;
        frist = frist.next;
        if(frist == null){
            last = null;
        }
        return value;
    }

    private static class Node<E>{
        private final E value;
        private Node<E> next;

        public Node(E value, Node<E> next) {
            this.value = value;
            this.next = next;
        }
    }
}
